package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Consumption;
import cn.wolfcode.enums.ConsumptionStatusEnum;
import cn.wolfcode.qo.ConsumptionQueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionItemService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.qo.JsonResult;
import com.github.pagehelper.PageInfo;
import cn.wolfcode.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {

    @Autowired
    private IConsumptionService consumptionService;
    @Autowired
    private IBusinessService businessService;
    @Autowired
    private IConsumptionItemService consumptionItemService;

    @RequiredPermission(name = "结算单页面", expression = "consumption:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ConsumptionQueryObject qo){
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("items", ConsumptionStatusEnum.values());
        model.addAttribute("businesses", businessService.listAll());
        return "consumption/list";
    }

    @RequiredPermission(name = "结算单页面", expression = "consumption:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            consumptionService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "结算单页面", expression = "consumption:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Consumption consumption){
        if (consumption.getId() != null) {
            consumptionService.update(consumption);
        }else {
            consumptionService.save(consumption);
        }
        return new JsonResult();
    }

    // 处理去保存或修改请求 /consumption/input
    @RequiredPermission(name="员工新增/编辑", expression = "consumption:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        Consumption consumption = consumptionService.get(id);
        if (id != null) {
            model.addAttribute("consumption", consumption);
            model.addAttribute("items", consumptionItemService.selectByCno(consumption.getCno()));
        }
        model.addAttribute("business", businessService.listAll());
        return "consumption/input";
    }

    @RequestMapping("/save")
    @RequiredPermission(name = "结算单页面", expression = "consumption:save")
    @ResponseBody
    public JsonResult save(String appointmentAno){
        Consumption consumption = consumptionService.save(appointmentAno);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(consumption.getId());
        return jsonResult;
    }
}
